﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;
using WatiN.Core;
using WatiN.Core.UtilityClasses;

namespace AutoDownload
{
    public partial class Download : System.Windows.Forms.Form
    {
        public Download()
        {
            InitializeComponent();
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            //http://tailieu.vn/xem-tai-lieu/don-chinh-sach-tien-te-toi-diem-chet-.1302742.html
            if (linkText.Text == "")
            {
                MessageBox.Show(this, "Must enter valid url", "Auto Download", MessageBoxButtons.OK);
                return;
            }
            var browser = new IE(linkText.Text);
            var loginLink = browser.Link(l => l.Text == "Đăng nhập");
            if (loginLink.Exists)
            {
                var loginPage = browser.Page<LoginPage>();

                loginLink.Click();
                loginPage.LoginWith("dhvan85@yahoo.com", "tailieu2121985");
                browser.GoTo(linkText.Text);
            }
            browser.WaitForComplete();
            var imageLink = browser.Link(Find.ByUrl(l => l.Contains(@"http://tailieu.vn/download/document")));
            
            
            Trace.WriteLineIf(!imageLink.Exists, imageLink.Url);
            Trace.WriteLineIf(!imageLink.Exists, browser.Url);

            if (imageLink.Exists)
            {
                imageLink.Click();
                imageLink.WaitForComplete();
            }

            //var capture = new CaptureWebPage(imageLink.DomContainer);
            //var image = capture.CaptureWebPageImage(true, true, 100);
            //pictureBox1.Image = image;
        }
    }
}
